package com.example.statistics.ui.bank

import android.os.Bundle
import android.view.View
import com.example.statistics.R
import com.example.statistics.bean.StatementBean
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.goTimeFormat
import com.example.statistics.ex.httpGet
import com.example.statistics.ex.onActionLongClicked
import com.example.statistics.ex.orEmpty
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityForResult
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector
import kotlinx.android.synthetic.main.layout_search_bar.*
import org.jetbrains.anko.toast

class StatementListActivity : BaseLoadingListActivity<StatementBean>() {
    private var functionId: Int = 0
    private var function: String = ""

    override val title: String
        get() = "账单对账详情"

    override val url: String
        get() = UrlConfig.BANK_STATEMENT_LIST

    override val itemLayoutRes: Int
        get() = R.layout.list_item_common_display

    override val inputSearch: Boolean
        get() = true



    override val httpParams: MutableList<Pair<String, String>>
        get() {
            val params = mutableListOf<Pair<String, String>>()
            functionId = intent.getIntExtra(Constants.FUNCTION_ID, 0)
            function = intent.getStringExtra(Constants.FUNCTION).orEmpty()

            if (functionId == 0 || function.isEmpty()) {
                finish()
                return params
            }
            params.add("function" to function)
            params.add("functionId" to functionId.toString())
            return params
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val money = intent.getDoubleExtra(Constants.DATA, 0.0)

        topBarRightImage {
            var statistics = 0.0
            dataList.forEach {
                statistics += it.money
            }
            if (statistics == money) {
                toast("没有可用余额")
                return@topBarRightImage
            }

            activityForResult<StatementAddActivity>(
                Constants.FUNCTION to function,
                Constants.FUNCTION_ID to functionId,
            ) {
                refresh()
            }
        }
    }

    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: StatementBean, position: Int) {
        injector.text(R.id.tv_title, bean.bank)
            .text(R.id.tv_subtitle, bean.cardNumber)
            .text(R.id.tv_bottom_right, bean.date.goTimeFormat("yyyy-MM-dd HH:mm"))
            .text(R.id.tv_status, bean.money.toString())
            .onActionLongClicked(
                "删除" to {
                    httpGet<StatementBean>(UrlConfig.BANK_STATEMENT_DELETE,
                        config = {
                            listParams = listOf("id" to bean.ID)
                        }) {
                        refresh()
                    }
                }
            )
    }
}